<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/7
 * Time: 12:43
 */

namespace App\Process;

use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Curl;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\HttpClient\HttpClient;
use EasySwoole\RedisPool\Redis;

class CsgoLive extends AbstractProcess
{
    public function run($arg)
    {
        go(function ()use($arg){
            \EasySwoole\Component\Timer::getInstance()->loop(1000, function () {
                Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis){
                    $battle_ids = [];
                    $livescorecsgo = UrlEnums::FJURL.UrlEnums::CSGO_LIVE_MATCH_PATH.'?version=2';
                    $csgo_match = new HttpClient($livescorecsgo);
                    list($s1, $s2) = explode(' ', microtime());
                    $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                    $option = [
                        'path'  => UrlEnums::CSGO_LIVE_MATCH_PATH,
                        'api_time'  => $microtime
                    ];
                    $header = Auth::getInstance($option)->header($option);
                    $csgo_match->setHeaders($header);
                    $response = $csgo_match->get();
                    $data = $response->getBody();
                    $data = json_decode($data,true);
                    if( $data['code'] == 200 && !empty($data['data']) ){
                        $redis->set('livescorecsgo',$data['data']); //将对局即时比分存入redis
                        foreach ( $redis->get('livescorecsgo') as $item ){
                            $battle_ids = array_merge($battle_ids,$item['battle_ids']); //获取battle_id
                        }
                        if( !empty($battle_ids) ){
                            foreach ( $battle_ids as $battle_id ) {//根据battle_id，请求实时比分数据，并redis保存
                                $livebattle = UrlEnums::FJURL.UrlEnums::CSGO_LIVE_BATTLE_PATH."?battle_id={$battle_id}&version=2";
                                $live_battle = new HttpClient($livebattle);
                                list($s1, $s2) = explode(' ', microtime());
                                $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
                                $option = [
                                    'path'      => UrlEnums::CSGO_LIVE_BATTLE_PATH,
                                    'api_time'  => $microtime
                                ];
                                $header = Auth::getInstance()->header($option);
                                $live_battle->setHeaders($header,true,false);
                                $response = $live_battle->get();
                                $battle_data = $response->getBody();
                                $battle_data = json_decode($battle_data,true);
                                $redis->set('livecsgo_battle_'.$battle_id,$battle_data);
                            }
                        }
                    }
                });
            });
        });
    }
}